<?php

namespace Core\init;

use Core\annotations\Bean;
use Core\lib\RedisPool;

/**
 * Class PhpRedisPool
 * @Bean
 */
class PhpRedisPool extends RedisPool
{
    public function __construct($min = 5, $max = 10, $isDelTime = 10)
    {
        global $GLOBALS_CONFIGS;
        if (!isset($GLOBALS_CONFIGS['redis_pool']) || empty($GLOBALS_CONFIGS['redis_pool'])) {
            throw new \Exception("未设置连接池配置参数");
        }
        $config = $GLOBALS_CONFIGS['redis_pool'];
        $min = $config['min'];
        $max = $config['max'];
        $isDelTime = $config['isdeltime'];
        parent::__construct($min, $max, $isDelTime);
    }

    public function newRedis()
    {
        global $GLOBALS_CONFIGS;
        if (!isset($GLOBALS_CONFIGS['redis']) || empty($GLOBALS_CONFIGS['redis'])) {
            throw new \Exception("未设置redis相关配置");
        }
        $redis = new \Redis();
        $redis->connect($GLOBALS_CONFIGS['redis']['host'], $GLOBALS_CONFIGS['redis']['port']);
        if (isset($GLOBALS_CONFIGS['redis']['auth']) && !empty($GLOBALS_CONFIGS['redis']['auth'])) {
            $redis->auth($GLOBALS_CONFIGS['redis']['auth']);
        }
        return $redis;
    }

}
